/**
 * Created by ROGSTAR on 2016/11/29.
 */

require.config(window.jshead);
require(['jquery', 'layer', 'navigator'], function ($, layer) {
    layer.config({
        path: '/js/layer/'
    });
    //风险评测

    //展开
    $('.show_modify_modal').on('click', function () {
        var slideOne = $(this).parent().next();
        slideOne.slideToggle();
        $('.panel_area').not(slideOne).slideUp();

    });
    function getInfo() {
        $.ajax({
            type: "POST",
            url: "/user/security/information",
            dataType: "json",
            success: function (data) {
                $('#payPhone').val(data.user.mobile);
                $('#phone').html(data.user.mobile);
                $('#oldphone').html(data.user.mobile);
            }
        });
    }
    getInfo();
    var grade = '';
    for (var i = 0; i < $("#gradeStar").val(); i++) {
        grade += '<img src="/img/xingxing1.png"/>';
    }
    for (var i = 0; i < 5 - $("#gradeStar").val(); i++) {
        grade += '<img src="/img/xingxing.png"/>';
    }
    $("#grade").html(grade);
    //跳转
    $('.show_modify_modal11').on('click', function () {
        $.ajax({
            type: "POST",
            url: "/user/security/doRealname",
            data: {
                bizType: '01'
            },
            dataType: "html",
            success: function (data) {
                var newwin = window.open('', '', '');
                newwin.opener = null;
                newwin.document.write(data);
                newwin.document.close();

            }
        });
    });
    $('.show_modify_modal12').on('click', function () {
        $.ajax({
            type: "POST",
            url: "/user/security/doRealname",
            data: {
                bizType: '01'
            },
            dataType: "html",
            success: function (data) {
                var newwin = window.open('', '', '');
                newwin.opener = null;
                newwin.document.write(data);
                newwin.document.close();

            }
        });
    });
    //修改手机号码
    function changeMobile() {
        var mobile = $('#newMobile').val();
        var inputRandomCode = $('#inputRandomCode').val();
        $.ajax({
            type: "POST",
            url: "/user/modifyMobile",
            data: {
                mobile: mobile,
                inputRandomCode: inputRandomCode
            },
            dataType: "json",
            success: function (data) {

            }
        });
    }

//旧验证码
    $('#oldclickMes').bind('click', function () {
        var oldphone = $('#oldphone').html();
        if (oldphone == "") {
            layer.tips("请先输入手机号", '#oldphone');
            return;
        }
        time($(this));
        //短信接口
        $.ajax({
            type: "POST",
            url: "/user/oldModifyMobileSms",
            data: {
                mobile: oldphone,
            },
            dataType: "json",
            success: function (data) {

            }
        });
        //短信接口end
    });
//新验证码
    $('#newclickMes').bind('click', function () {
        var newphone = $('#newphone').val();
        var oldCode = $('#oldCode').val();
        if (newphone == "") {
            layer.tips("请先输入手机号", '#newphone');
            return;
        }
        //短信接口
        time($('#newclickMes'));
        $.ajax({
            type: "POST",
            url: "/user/newModifyMobileSms",
            data: {
                mobile: newphone,
                inputCode: oldCode
            },
            dataType: "json",
            success: function (data) {

                if (data.result == 'success') {
                    layer.msg("短信已发送！");
                } else {
                    layer.tips(data.messageText, '#oldCode');
                }
            }
        });
        //短信接口end
    })


    var wait = 60;

    function time(o) {
        if (wait == 0) {
            o.removeAttr("disabled");
            o.val('获取验证码');
            o.css("color", '#ffffff');
            o.css("background","#2c97f4");
            wait = 60;
        } else {
            o.attr("disabled", true);
            o.css("color", '#fff');
            o.css("background", '#ddd');
            o.val("重新发送(" + wait + "s)");
            wait--;
            setTimeout(function () {
                    time(o)
                },
                1000)
        }
    }

    $('#modifyLoginPass').bind('click', function () {
        var newphone = $('#newphone').val();
        var inputRandomCode = $('#inputRandomCode').val();
        $.ajax({
            type: "POST",
            url: "/user/modifyMobile",
            data: {
                mobile: newphone,
                inputRandomCode: inputRandomCode
            },
            dataType: "json",
            success: function (data) {

                if (data.result == 'success') {
                    layer.alert('修改成功', function () {
                        window.location.reload();
                    });
                } else {
                    layer.alert(data.messageText);
                }
            }
        });
    });


//修改密码

    $('#modify').bind('click', function () {
        var oldpassword = $('#oldpassword').val();
        var newpassword = $('#newpassword').val();
        var newpasswordConfirm = $('#newpassword1').val();
        if (newpassword.length < 6) {
            layer.tips("新密码不能小于6位", '#newpassword');
            return;
        }
       if(newpassword != newpasswordConfirm){
           layer.tips("两次密码确认不一致", '#newpassword1');
           return;
       }
        $.ajax({
            type: "POST",
            url: "/user/modifyPassword",
            data: {
                oldPassword: oldpassword,
                newPassword: newpassword
            },
            dataType: "json",
            success: function (data) {

                if (data.result == 'success') {
                    layer.alert('修改成功,即将重新登录')
                    setTimeout(function () {
                        window.location.href = "/loginOut";
                    }, 3000);
                } else {
                    layer.alert(data.messageText)
                }
            }
        });
    });


    //修改交易密码
    $("#payPasswordBtn").bind("click", function () {
        var idCard = $('#idCard').val();
        var newPayPassword = $('#newPayPassword').val();
        var newPayPassword2 = $('#newPayPassword2').val();
        var mobileCode = $('#mobileCode').val();
        if (idCard == '') {
            layer.tips("请输入您的身份证号码", '#idCard');
            return;
        }else if(idCard.length<18){
            layer.tips("请输入正确身份证号码", '#idCard');
            return;
        }
        if (newPayPassword == '') {
            layer.tips("请输入6位交易密码", '#newPayPassword');
            return;
        }
        if (newPayPassword != newPayPassword2) {
            layer.tips("2次交易密码不一致", '#newPayPassword2');
            return;
        }
        if (mobileCode == '') {
            layer.tips("请输入短信验证码", '#mobileCode');
            return;
        }
        $.ajax({
            type: "POST",
            url: "/user/modifyPayPassword",
            dataType: "json",
            data: {
                newPayPassword: newPayPassword,
                mobileCode: mobileCode,
                idCard:idCard,
            },
            success: function (data) {
                console.log(data);
                if (data.result == 'success') {
                    layer.alert('修改成功', function () {
                        window.location.reload();
                    });
                } else {
                    layer.alert(data.messageText);
                }
            }
        });
    });
    //支付密码验证码
    $('#payclickMes').bind('click', function () {
        var payPhone = $('#payPhone').val();
        time($(this));
        //短信接口
        $.ajax({
            type: "POST",
            url: "/doModifyPayPasswordSms",
            data: {
                mobile: payPhone,
            },
            dataType: "json",
            success: function (data) {
                if(data.result=='success'){
                    layer.msg("验证码已发送");
                }else{
                    layer.alert(data.messageText);
                }
            }
        });
        //短信接口end
    });


    //解绑银行卡
    $('#unbundling').bind('click', function () {
        $('.rebox').show(200);
        $('#cancel1').bind('click', function () {
            $('.rebox').hide();
        })
        $('#determine1').bind('click', function () {
            var z = /^[0-9]*$/
            var transactionPassword = $('#inputCode1').val();
            if (transactionPassword == '') {
                layer.tips('请输入交易密码', '#inputCode1');

            } else if (!z.test(transactionPassword)) {
                layer.tips('交易密码有数字组成', '#inputCode1')
            } else if (transactionPassword.length != 6) {
                layer.tips('交易密码有6位数字组成', '#inputCode1')
            } else {
                $.ajax({
                    type: 'POST',
                    url: '/user/security/cardUnbund',
                    dataType: "json",
                    data: {
                        payPassword: transactionPassword
                    },
                    success: function (data) {
                        if (data.result == 'fail'){
                             layer.alert(data.messageText + '解绑失败')
                        } else {
                            layer.msg('解绑'+data.msg);
                            setTimeout(function () {
                                window.location.href = 'security?4?8'
                            }, 3000);
                        }
                    }
                })
            }
        })


    });
    //校验身份证
    $('#idCard').blur(function(){
        var code = $('#idCard').val();
        if(!code || !/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i.test(code)){
            layer.tips('身份证格式错误','#idCard');
            $('#idCard').val('')
            return;
        }
    })
    // $('#idCard').bind('blur',function(){
    //    var  isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    //     var idCard= $('#idCard').val()
    //     if(!isIDCard1.test(idCard)){
    //         layer.tips('请输入正确的身份证','#idCard')
    //         $('#idCard').val('')
    //     }
    // })
});